﻿@model yule.CodeBuilder.Core.PDM.TableInfo
@{
    yule.CodeBuilder.Core.IBuilder baseModeleBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model,yule.CodeBuilder.Core.BuildType.BaseModel);
    yule.CodeBuilder.Core.IBuilder iDALBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.IDAL);
    yule.CodeBuilder.Core.IBuilder modeleBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.Model);
}
//////////////////////////////////
///生成时间:@System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
///BaseModel:@(string.Format("{0}({1})",Model.Name,Model.Core ))
//////////////////////////////////
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace @baseModeleBuilder.NameSpace
{
    /// <summary>
    /// @Model.Name (@Model.Core)
    /// </summary>
    [DataContract]
    [Serializable]
    public abstract class @baseModeleBuilder.ClassName : yule.Model.BaseModel<@(modeleBuilder.ClassFullName)>,IModel
    {
        #region 属性

        @foreach (var column in Model.Columns)
        {
        @:/// <summary>
        @:/// @column.Name
        @:/// </summary>
        @:private @column.CSharpType @("_" + column.Code) = @yule.CodeBuilder.Core.PDM.pgConvert.GetCSharpDefaultValue(column.CSharpType);
        @:/// <summary>
        @:/// @column.Name @column.DataType
        @:/// </summary>
        @:[DataMember]
        @:public @column.CSharpType @column.Code
        @:{
        @:    get { return @("this._" + column.Code); }
        @:    set { @("this._" + column.Code) = value; }
        @:}
        @:
        }
        #endregion

        #region 方法

        /// <summary>
        /// 转化实体
        /// </summary>
        public override void Convert(System.Data.DataRow row)
        {
            @foreach (var column in Model.Columns)
            {
            @:@("if (row.Table.Columns.Contains(" + modeleBuilder.ClassFullName + ".Field." + column.Code + ") == true)")
            @:@("{")
            @:@("    this." + column.Code + " = " + "yule.Util.Convert.To" + column.CSharpType.FirstUp() + "(row[" + modeleBuilder.ClassFullName + ".Field." + column.Code + "]);")
            @:@("}")
            }
        }
        
        public static @modeleBuilder.ClassFullName Select(string id)
        {

            @modeleBuilder.ClassFullName model = yule.Common.CacheManager.Get<@modeleBuilder.ClassFullName>(id);

            if (model == null)
            {
                model = yule.Common.ServiceManager.ServiceContext.CreateService<@iDALBuilder.ClassFullName>().Select(id);
            }

            return model;
        }

        #endregion

        #region 表

        /// <summary>
        /// 表
        /// </summary>
        public struct Table
        {
            /// <summary>
            /// 表名
            /// </summary>
            public const string Name = "@Model.Core";
        }

        #endregion 

        #region 字段

        /// <summary>
        /// 字段
        /// </summary>
        public struct Field
        {
            @foreach (var column in Model.Columns)
            {
            @:/// <summary>
            @:/// @column.Name
            @:/// </summary>
            @:public const string @column.Code = "@column.Code";
            }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 属性
        /// </summary>
        public struct Property
        {
            @foreach (var column in Model.Columns)
            {
            @:/// <summary>
            @:/// @column.Name
            @:/// </summary>
            @:public const string @column.Code = "@column.Code";
            }
        }

        #endregion

    }
}
